//
//  Controller04.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/1.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

class Controller04: UIViewController,UIScrollViewDelegate {

    var isShowNar = false {
        willSet{
            navigationController?.setNavigationBarHidden(newValue, animated: true);
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red;
        configViews();
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        isShowNar = true;
    }
    
    func configViews() {
        
        let controllers = [Controller04_1(),Controller04_2(),Controller04_3()];
        
        var sv : UIScrollView;
        do{
            sv = UIScrollView(frame: UIScreen.main.bounds);
            view.addSubview(sv);
            sv.contentSize = CGSize(width: SCREEN_W * CGFloat(controllers.count), height: SCREEN_H);
            sv.backgroundColor = .orange;
            sv.isPagingEnabled = true;
            sv.delegate = self;
            
            let label = UILabel(frame: CGRect(x: -80, y: SCREEN_H * 0.5 - 80, width: 20, height: 160));
            label.text = "继续滑动返回";
            sv.addSubview(label);
            label.backgroundColor = .gray;
            label.numberOfLines = 0;
            label.textColor = .white;
        }
 
        for (index,obj) in controllers.enumerated() {
            self.addChildViewController(obj);
            sv.addSubview(obj.view);
            obj.didMove(toParentViewController: self);
            
            var rect = UIScreen.main.bounds;
            rect.origin.x = CGFloat(index) * SCREEN_W;
            obj.view.frame = rect;
        }
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        let p = scrollView.contentOffset;
//        print("p : \(p.x)");
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let p = scrollView.contentOffset;
        print("p : \(p.x)");
        if p.x < -100 {
            _ = navigationController?.popViewController(animated: true);
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
